import Vue from 'vue'
import VueRouter from 'vue-router'
import VueMoment from 'vue-moment'
import { store } from './store/store'

import Main from './editor/Main.vue'

import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'

Vue.use(ElementUI)

import mavonEditor from 'mavon-editor'
import 'mavon-editor/dist/css/index.css'

// use
Vue.use(mavonEditor)

Vue.use(VueRouter)
Vue.use(VueMoment)

var routes = [
  {
    path: '/',
    component: Main,
    meta: {
      index: 1,
    },
  },
]

// V3版本：使用消息通信获取数据库实例
var db = null

// 异步获取数据库实例
function initDB() {
  return new Promise((resolve) => {
    chrome.runtime.sendMessage({ action: 'getDB' }, (response) => {
      if (response && response.db) {
        db = response.db
        window.db = db
        resolve(db)
      } else {
        console.error('获取数据库实例失败')
        resolve(null)
      }
    })
  })
}

// 初始化数据库
initDB().then((database) => {
  if (database) {
    console.log('数据库实例初始化成功')
  } else {
    console.error('数据库实例初始化失败')
  }
})

var router = new VueRouter({
  routes,
})
const app = new Vue({
  router,
  store,
})
app.$mount('#app')
